﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModelRelationship.ModelRelationship
{
    public class MainEntity
    {
        public int Id { get; set; }
        public List<SubEntity> SubEntities { get; set; } = new List<SubEntity>();
    }

    public class SubEntity
    {
        public string Text { get; set; }
    }

    /// <summary>
    /// 从属实体类型的集合
    /// </summary>
    public class OneToManyRelationship : DbContext
    {
        public DbSet<MainEntity> MainEntities => Set<MainEntity>();

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<MainEntity>(b =>
            {
                b.OwnsMany(m => m.SubEntities, s =>
                {
                    s.WithOwner().HasForeignKey("MainEntityId");
                    s.Property<int>("Id");
                    s.HasKey("Id");
                });
            });
        }

    }
}
